﻿using System.Windows.Forms;
using HuaCheng.CommonClass;
using HuaCheng.DataAccess;

namespace HuaCheng.Views
{
    /// <summary>
    /// 用户登录
    /// </summary>
    public partial class UserLogin : Form
    {
        public static bool success;
        public UserLogin()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 取消按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CancelBtn_Click(object sender, System.EventArgs e)
        {
            this.Close();
        }
        /// <summary>
        /// 登录按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void SubmitBtn_Click(object sender, System.EventArgs e)
        {
            if (tbUsername.Text.Trim() == "")
            {
                 MessageBox.Show("用户名不能为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else if (tbPwd.Text.Trim() == "")
            {
                 MessageBox.Show("密码不能为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else if(UserInfoHelper.IsExist(tbUsername.Text)==false)
            {
                MessageBox.Show("用户名错误！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else if (tbPwd.Text != UserInfoHelper.RetrieveUserinfo(tbUsername.Text).PassWord)
            {
                MessageBox.Show("密码错误！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                this.Close();
                success = true;
                IndexPage.username = tbUsername.Text;
                IndexPage indxp;
                indxp = (IndexPage)this.Owner;
                indxp.reLoad_Method();
            }
        }
        /// <summary>
        /// 按回车直接登录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pwdTb_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Return)
            {
                SubmitBtn_Click(null, null);
            }
        }
        #region 国际化
        /// <summary>
        /// 加载页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UserLogin_Load(object sender, System.EventArgs e)
        {
            ResourceCulture.SetCurrentCulture(IndexPage.language);
            this.SetResourceCulture();
        }
        /// <summary>
        /// 为当前页面内所有控件设置文本语种
        /// </summary>
        private void SetResourceCulture()
        {
            this.Text = ResourceCulture.GetString("LoginText");
            this.lblUsername.Text = ResourceCulture.GetString("UserNameText");
            this.lblPwd.Text = ResourceCulture.GetString("PasswordText");
            this.btnUsername.Text = ResourceCulture.GetString("LoginOk");
            this.btnPwd.Text = ResourceCulture.GetString("LoginCancel");
        }
        #endregion

        
    }
}
